﻿Imports NMock2

Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Meiners.Libs.Withings
Imports Meiners.Libs.Withings.Core

'''<summary>
'''This is a test class for SubscriptionManagerTest and is intended
'''to contain all SubscriptionManagerTest Unit Tests
'''</summary>
<TestClass()> _
Public Class SubscriptionManagerTest

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property



    '''<summary>
    '''A test for RevokeSubscription
    '''</summary>
    <TestMethod()> _
    Public Sub RevokeSubscriptionTest()
        Dim userID As Integer = 29
        Dim publicKey As String = "b71d95d5fb963458"
        Dim callbackURL As String = "http://www.yourdomain.net/yourCustomApplication.php"
        Dim application As DeviceTypeEnum = DeviceTypeEnum.BodyScale
        Dim expected As SetSubscriptionResponse = Nothing ' TODO: Initialize to an appropriate value
        Dim actual As SetSubscriptionResponse

        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)

        'Alle paramteter sollten anständig übergeben werden

        Expect.Once.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/notify?action=revoke&userid=29&publickey=b71d95d5fb963458&callbackurl=http%3a%2f%2fwww.yourdomain.net%2fyourCustomApplication.php&appli=1"}).Will([Return].Value(My.Resources.probe_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        actual = SubscriptionManager.RevokeSubscription(userID, publicKey, callbackURL, application)
        mocks.VerifyAllExpectationsHaveBeenMet()

        Assert.IsTrue(actual.Success, "retval is not correct")
    End Sub

    '''<summary>
    '''A test for ReadSubscription
    '''</summary>
    <TestMethod()> _
    Public Sub ReadSubscriptionTest()
        Dim userID As Integer = 29
        Dim publicKey As String = "b71d95d5fb963458"
        Dim callbackURL As String = "http://www.yourdomain.net/yourCustomApplication.php"
        Dim application As DeviceTypeEnum = DeviceTypeEnum.BodyScale
        Dim expected As ReadSubscriptionResponse = Nothing ' TODO: Initialize to an appropriate value
        Dim actual As ReadSubscriptionResponse


        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)

        'Alle paramteter sollten anständig übergeben werden

        Expect.Once.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/notify?action=get&userid=29&publickey=b71d95d5fb963458&callbackurl=http%3a%2f%2fwww.yourdomain.net%2fyourCustomApplication.php&appli=1"}).Will([Return].Value(My.Resources.getNotiy_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        actual = SubscriptionManager.ReadSubscription(userID, publicKey, callbackURL, application)
        mocks.VerifyAllExpectationsHaveBeenMet()

        Assert.IsInstanceOfType(actual, GetType(ReadSubscriptionResponse), "instance type is not correct")
        Assert.AreEqual(Of DateTime)(Helper.EPOCHToDateTime(2147483647), actual.ExpireDate, "ExpireDate is not correct")
        Assert.AreEqual(Of String)("Your Own Application Description", actual.Comment, "Comment property is not correct")


    End Sub

    '''<summary>
    '''A test for CreateSubscription
    '''</summary>
    <TestMethod()> _
    Public Sub CreateSubscriptionTest()
        Dim userID As Integer = 29
        Dim publicKey As String = "b71d95d5fb963458"
        Dim callbackURL As String = "http://www.yourdomain.net/yourCustomApplication.php"
        Dim comment As String = "Your Own Application Description"
        Dim application As DeviceTypeEnum = DeviceTypeEnum.BodyScale
        Dim expected As SetSubscriptionResponse = Nothing ' TODO: Initialize to an appropriate value
        Dim actual As SetSubscriptionResponse


        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As IHttpConnector = Nothing

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)

        Expect.Once.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/notify?action=subscribe&userid=29&publickey=b71d95d5fb963458&callbackurl=http%3a%2f%2fwww.yourdomain.net%2fyourCustomApplication.php&appli=1&comment=Your%20Own%20Application%20Description"}).Will([Return].Value(My.Resources.probe_SampleResult))
        WBSAPIConnector.WebConnector = webMock

        actual = SubscriptionManager.CreateSubscription(userID, publicKey, callbackURL, comment, application)
        mocks.VerifyAllExpectationsHaveBeenMet()

        Assert.IsTrue(actual.Success, "Success property is not correct")
        Assert.AreEqual(Of StatusResponseEnum)(StatusResponseEnum.Successfull, actual.Status, "Status Property is not correct")


    End Sub



End Class

